package com.vf.admin.client.controller.oppo.service;

import com.alibaba.fastjson.JSONObject;
import com.vf.admin.client.controller.oppo.OppoEncodeUtil;
import com.vf.admin.utils.Md5Util1;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;

/**
 * <p>Title : </p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : zxw
 * @Date 2023/8/26 10:29
 */
@Service
public class OppoPushService {

    public JSONObject sendInfoMsg(String oaid, String dataType) throws UnsupportedEncodingException, GeneralSecurityException {

        //salt
        String salt = "e0u6fnlag06lc3pl";
        //时间戳
        long timestamp = System.currentTimeMillis();


        //ouId加密
        String imei = OppoEncodeUtil.encode(oaid.getBytes("UTF8"));
        System.out.println(imei);

        //postData
        JSONObject postData = new JSONObject();
        postData.put("imei", imei);//imei oaid
        postData.put("timestamp", timestamp);//时间戳
        postData.put("pkg", "com.ptszyxx.zhiyu.app");
        postData.put("dataType", dataType);
        postData.put("channel", 1);
        postData.put("type", 2);
        postData.put("ascribeType", 1);
        postData.put("adId", 12345678);
        System.out.println(postData);

        //1.2 Signature 签名
        //使用 md5 对数据进行签名: md5( postData + timestamp + salt)，md5 后转小写
        String signature = postData + String.valueOf(timestamp) + salt;
        String hex = Md5Util1.md5Hex(signature);
        System.out.println(hex);


        try {
            URL urlObj = new URL("https://api.ads.heytapmobi.com/api/uploadActiveData");
            HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();

            // 设置请求方法为POST
            connection.setRequestMethod("POST");

            // 允许写入数据
            connection.setDoOutput(true);

            // 设置请求体的内容类型为表单数据
            connection.setRequestProperty("signature", hex);
            connection.setRequestProperty("timestamp", String.valueOf(timestamp));
            connection.setRequestProperty("Content-Type", "application/json");

            // 获取输出流并写入请求参数数据
            DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(postData.toJSONString());
            outputStream.flush();
            outputStream.close();

            // 获取响应状态码
            int responseCode = connection.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 读取响应数据
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
                String line;
                StringBuilder response = new StringBuilder();

                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }

                reader.close();

                // 输出响应结果
                System.out.println("请求成功，响应结果为：" + response.toString());
                JSONObject jsonObject = JSONObject.parseObject(response.toString());
                return jsonObject;
            } else {
                System.out.println("请求失败，错误码为：" + responseCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
}

